<?php namespace app\api\controller;

use app\common\controller\AuthAndBind;
use think\Controller;
use think\Db;
use alisms\SendSms;
use esign\EsignHandle;

/**
 * api
 */
class Preach extends AuthAndBind{
	
	public function __construct(){
		parent::__construct();
	}
	
	/**
	 * 批量删除
	 */
	public function batchDelete(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where[] = ['id', 'in', $in['id']];
				$oldData = Db::table("gd_preach") -> where($where) -> select();
				
				if( !empty($oldData) ){
					Db::startTrans();
					foreach ($oldData as $k => $v) {
						if( $v['is_delete'] != 1 ){
							$save['is_delete'] = 1;
							$delRes = Db::table("gd_preach") -> where(['id'=>$v['id']]) -> update($save);
							if( !$delRes ) {
								Db::rollback();
								returnResults(['code'=>'-1', 'msg'=>'操作失败,请联系管理员']);
							}
						}
					}
					Db::commit();
				}
				
				$outArr['code'] = '1';
				
//				写入操作日志
				$old_data_json = $oldData;
				controller("service/OperLog") -> logs(
					[
						'title'=>'批量删除专家宣讲',
						'business_type'=>2,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 详情
	 */
	public function details(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where[] = ['id', 'eq', $in['id']];
				
				$data = Db::table("gd_preach") -> where($where) -> find();
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'获取专家宣讲详情',
						'business_type'=>4,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 列表
	 */
	public function index(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$num = isset($in['num']) ? $in['num'] : 20;
				$start = isset($in['page']) && $in['page'] > 0 ? (($in['page'] - 1) * $num) : 0;
				
				$where[] = ['is_delete', 'eq', 0];
				if( isset($in['create_date']) ) $where[] = ['create_date', 'between', $in['create_date']];
				if( isset($in['type']) ) $where[] = ['type', 'eq', $in['type']];
				if( isset($in['name']) ) $where[] = ['name', 'like', '%'.trim($in['name']).'%'];
				if( isset($in['create_user_id']) ) $where[] = ['create_user_id', 'eq', $in['create_user_id']];
				
				$data = Db::table("gd_preach") -> where($where) -> order("id desc") -> limit($start, $num) -> select();
				$count = Db::table("gd_preach") -> where($where) -> count();
				
				if( !empty($data) ){
					$goods = Db::table("gd_goods") -> where([['id', 'in', array_column($data, 'goods_id')]]) -> select();
					if( !empty($goods) ){
						foreach ($goods as $k => $v) {
							$goods[$k]['min_price_protection'] = Db::table("gd_goods_spec") -> where(['goods_id'=>$v['id'], 'is_delete'=>0]) -> min("price");
							$goods[$k]['max_price_protection'] = Db::table("gd_goods_spec") -> where(['goods_id'=>$v['id'], 'is_delete'=>0]) -> max("price");
							$goods[$k]['min_price'] = Db::table("gd_goods_spec") -> where(['goods_id'=>$v['id'], 'is_delete'=>0]) -> min("price");
							if( !empty($v['class_id1']) ) $goods[$k]['calssName'] = Db::table("gd_classification") -> where(['id'=>$v['class_id1']]) -> find()['name'];
							if( !empty($v['class_id2']) ) $goods[$k]['calssName'] = Db::table("gd_classification") -> where(['id'=>$v['class_id2']]) -> find()['name'];
							if( !empty($v['class_id3']) ) $goods[$k]['calssName'] = Db::table("gd_classification") -> where(['id'=>$v['class_id3']]) -> find()['name'];
						}
					}
					$auction = Db::table("gd_auction") -> where([['id', 'in', array_column($data, 'auction_id')]]) -> select();
					if( !empty($auction) ){
						foreach ($auction as $k => $v) {
							if( !empty($v['class_id1']) ) $auction[$k]['calssName'] = Db::table("gd_classification") -> where(['id'=>$v['class_id1']]) -> find()['name'];
							if( !empty($v['class_id2']) ) $auction[$k]['calssName'] = Db::table("gd_classification") -> where(['id'=>$v['class_id2']]) -> find()['name'];
							if( !empty($v['class_id3']) ) $auction[$k]['calssName'] = Db::table("gd_classification") -> where(['id'=>$v['class_id3']]) -> find()['name'];
						}
					}
					
					foreach ($data as $k => $v) {
						$data[$k]['goods'] = [];
						$data[$k]['auction'] = [];
						if( !empty($goods) ){
							foreach ($goods as $kk => $vv) {
								if( $v['goods_id'] == $vv['id'] ) {
									$data[$k]['goods'] = $vv;
									$data[$k]['goodsName'] = $vv['name'];
									$data[$k]['goodsClassName'] = isset($vv['calssName']) ? $vv['calssName'] : '';
									$data[$k]['min_price_protection'] = isset($vv['min_price_protection']) ? $vv['min_price_protection'] : '';
								}
							}
						}
						if( !empty($auction) ){
							foreach ($auction as $kk => $vv) {
								if( $v['auction_id'] == $vv['id'] ) {
									$data[$k]['auction'] = $vv;
									$data[$k]['goodsName'] = $vv['name'];
									$data[$k]['goodsClassName'] = isset($vv['calssName']) ? $vv['calssName'] : '';
									$data[$k]['min_price_protection'] = isset($vv['start_price']) ? $vv['start_price'] : '';
								}
							}
						}
					}
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				$outArr['totalNums'] = $count;				
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'获取专家宣讲列表',
						'business_type'=>4,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 编辑
	 */
	public function edit(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where[] = ['id', 'eq', $in['id']];
				$oldData = Db::table("gd_preach") -> where($where) -> find();
				if( empty($oldData) ) returnResults(['code'=>'-1', 'msg'=>'原数据有误']);
				
				$save = [];
				foreach ($in as $k => $v) {
					if( !in_array($k, ['id', 'user_id']) ){
						$save[$k] = is_array($v) ? json_encode($v, JSON_UNESCAPED_SLASHES) : $v;
					}
				}
				
				$save = filterArr($save, array_keys($save), $oldData);
				
				if( !empty($save) ){
					$re = Db::table("gd_preach") -> where($where) -> update($save);
					if( !$re ) returnResults2('-1', '编辑失败,请联系管理员');
				}
				$outArr['code'] = '1';
				
//				写入操作日志
				$old_data_json = $oldData;
				controller("service/OperLog") -> logs(
					[
						'title'=>'编辑专家宣讲',
						'business_type'=>3,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 新增
	 */
	public function add(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$newData = [];
				foreach ($in as $k => $v) {
					if( !in_array($k, ['id', 'user_id']) ){
						$newData[$k] = is_array($v) ? json_encode($v, JSON_UNESCAPED_UNICODE) : $v;
					}
					$newData['create_user_id'] = $in['user_id'];
					$newData['create_user_name'] = $userInfo['info']['nickname'];
					$newData['create_user_type'] = $userInfo['info']['type'];
					$newData['create_date'] = date("Y-m-d H:i:s", time());
				}
				$re = Db::table("gd_preach") -> insertGetId($newData);
				if( !$re ) returnResults2('-1', '新增失败,请联系管理员');
				$outArr['code'] = '1';
				$outArr['info'] = $re;
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'新增专家宣讲',
						'business_type'=>1,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
}

?>